
/*!
 * p4p page modifier
 *
 * Date: Thu July 18  2011
 */

//////////////////////////////////////////////////////////////////////////////////////////////
//				全局变量区
//////////////////////////////////////////////////////////////////////////////////////////////

var ret;
var cnt=0;

// utility function
function dirname(path) {
	return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
}

// Define column usage.
var colSearchTimes = 2;
var colAvgPrice = 7;
var colRank = 6;
var colCat = 3;
var colWord = 2;

var levelok='background:#6F6';
var level0='background:#B7FEA5';
var level1='background:#9AFED9';
var level2='background:#C0CEFE';
var level3='background:#D5A8FD';
var level4='background:#FEFE43';
var level5='background:#F56969';

// 获得全局的 url prefix
var url = document.location.href;	//获取当前tab文档的url 
var prefix = dirname(url);		//通过tab文档的url获取 http://subway.simba.taobao.com/bidword

// 获取当前推广的 宝贝id。
var auction_id=0;
if(url.search(/bidwordmanage\.htm/i)>=0){
	var auction_id = $('#adGroupTitle').parent().attr('href');
	if(typeof auction_id != 'undefined'){
		auction_id=auction_id.match(/\d+$/g).pop();
		if(typeof auction_id == 'undefined'){
			console.log("[WARN] Does not get auction_id.\n");
		}
	}
	
}

$('td[id*="realClkCnt"]').bind('mousedown', oneRowHandler);
$('#tb_keyword').on("click","td",  pageHandler);

//////////////////////////////////////////////////////////////////////////////////////////////
//				函数定义
//////////////////////////////////////////////////////////////////////////////////////////////
function auth() {
	var name='abc';
	var str=$('.headright p').eq(1).text();
	if(str.search(/：/)>=0){
		name=str.match(/：.*\)/gi).pop().slice(1,-1);
	} else {
		name=str.match(/，.*\(/gi).pop().slice(1,-1);
	}

	var status=0;
	var request = $.ajax({
	  url: "http://t.trenddata.cn/p4p.php",
	  async: false,
	  type: "GET",
	  data: {name : name},
	  success:function(html){
		  if('4528e6a7bb9341c36c425faf40ef32c3'==html){
			 status = 1;
		  } else {
			 status = 0;
		  }
		  if('95b3644556b48a25f3366d82b0e3b349'==html){
			 stats = 0;
		  }
	  }
	});
	
	return status;
}

function retrieve(from, to) {

    alert("hi");

	// 获取当前推广的 宝贝id。
	var auction_id = $('#adGroupTitle').parent().attr('href');
	if(typeof auction_id != 'undefined'){
		auction_id=auction_id.match(/\d+$/g).pop();
		if(typeof auction_id == 'undefined'){
			console.log("[WARN] Does not get auction_id.\n");
		}
	}

	var trs = $('tr[id*=delete]');
	if (from >= 0 && to >= 0) {
		trs = $('tr[id*=delete]').slice(from, to);
	}
	var count = 0;
    var total = trs.length;
    
	trs.each(function(i) {
		var word=$(this).children().eq(1).children().text();

		var url="http://subway.simba.taobao.com/insight/doWhois.htm";

		var tr = $(this);
		var rank=102; // 初始值设置为102；

		rank=retrieve_url(url, word, auction_id);			

		if(rank != 266){ //代表不是没有抓到的情况
			if (rank > 100) {
				rank = "101";
			}
			tr.children().eq(colRank).text(rank).addClass('n'+colRank);
			var color = "background:#F33";
			if(rank <= 100){
				color="background:#6F6";
			}
			tr.children().eq(colRank).attr('style', color);
		}
		
		count++;
		if (count == total) {
			resetTable();
		}

	});
}

var lastClickedCheckboxIdx;
function resetTable() {
	// $("#pio").trigger("update");
	
	$("#pio input[name=checkbox_bidword]").each(function() {
		$(this).replaceWith($(this).clone());
	});
	// $("input[name=checkbox_bidword]").shiftcheckbox();
	$("input[name=checkbox_bidword]").click(function() {
		var checked = $(this).is(":checked");
		var thisIdx = $(this).parent().parent().prevAll().length;
		if (lastClickedCheckboxIdx) {
			var min = Math.min(lastClickedCheckboxIdx, thisIdx);
			var max = Math.max(lastClickedCheckboxIdx, thisIdx);
			if (event.shiftKey) {
				if (checked) {
					$("input[name=checkbox_bidword]").each(function(idx) {
						if (idx >= min && idx <= max) {
							$(this).attr("checked", "checked");
						}
					});
				} else {
					$("input[name=checkbox_bidword]").each(function(idx) {
						if (idx >= min && idx <= max) {
							$(this).removeAttr("checked");
						}
					});
				}
			}
		}
		lastClickedCheckboxIdx = thisIdx;
	});
}

function shiftCheckbox() {
	
}

function init_header(){
	$('table.tablesorter.tb3>thead>tr').children().eq(colSearchTimes).text("搜索次数");
	$('table.tablesorter.tb3>thead>tr').children().eq(colAvgPrice).text("平均出价");
	$('table.tablesorter.tb3>thead>tr').children().eq(colRank).text("排名");
}


function init_sort_header(){
	var appendStr='\
	<tr id="myAppendRow"><td widtd="1%"></td> \
	<td align="center"></td>\
	<td widtd="60"  align="center"></td>\
	<td widtd="60"  align="center"></td>\
	<td widtd="60"  align="center"></td>\
	<td widtd="60"  align="center"></td>\
	<td widtd="60"  align="center"></td>\
	<td widtd="50"  align="center"></td>\
	<td widtd="70"  align="center"></td>\
	<td widtd="70"  align="center"></td>\
	<td widtd="68" align="center"></td>\
	</tr>';

	if($('#pio>thead>tr').length <= 1){
		$('#pio>thead>tr').after(appendStr);

		$('#myAppendRow').children().eq(colSearchTimes).append('<a href="javascript:;" id="myP4PAsc'+colSearchTimes+'">升</a>&nbsp;&nbsp;<a href="javascript:;" id="myP4PDsc'+colSearchTimes+'">降</a>');
		$('#myAppendRow').children().eq(colAvgPrice).append('<a href="javascript:;" id="myP4PAsc'+colAvgPrice+'">升</a>&nbsp;&nbsp;<a href="javascript:;" id="myP4PDsc'+colAvgPrice+'">降</a>');
		$('#myAppendRow').children().eq(colRank).append('<a href="javascript:;" id="myP4PAsc'+colRank+'">升</a>&nbsp;&nbsp;<a href="javascript:;" id="myP4PDsc'+colRank+'">降</a>');		
		$('#myAppendRow').children().eq(colCat).append('<span title="从好到坏匹配，深绿，浅绿，浅青，浅蓝，浅紫色，淡黄色，暗红色"><a>类目匹配度</a></span>');
	}

	$("#myP4PAsc"+colSearchTimes).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colSearchTimes,
			item: 'tr',
			is_num: true,
			order: 'asc'
		});
	});

	$("#myP4PDsc"+colSearchTimes).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colSearchTimes,
			item: 'tr',
			is_num: true,
			order: 'desc'
		});
	});

	$("#myP4PAsc"+colAvgPrice).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colAvgPrice,
			item: 'tr',
			is_num: true,
			order: 'asc'
		});
	});

	$("#myP4PDsc"+colAvgPrice).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colAvgPrice,
			item: 'tr',
			is_num: true,
			order: 'desc'
		});
	});

	$("#myP4PAsc"+colRank).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colRank,
			item: 'tr',
			is_num: true,
			order: 'asc'
		});
	});

	$("#myP4PDsc"+colRank).click(function(){
		$("#pio>tbody").jSort({
			sort_by: 'td.n'+colRank,
			item: 'tr',
			is_num: true,
			order: 'desc'
		});
	});

}

function retrieve_url(url, word, id){

		//开始 2011-12-01修改 增加类目匹配
		// 获得宝贝类目
		var cat_str=$("div.title>div").eq(0).text();
		var cat=cat_str.match(/：.*$/).pop().slice(1);
		var item_cat_arr=new Array();
		item_cat_arr=cat.replace(/\s/gi, '').split('>');
		//结束 2011-12-01修改 增加类目匹配

		token_value=document.location.href.split('&')[0].split('?')[1].split('=')[1];
		var rank=101; // 初始情况下，排名设置为101

		
		var html = $.ajax({
			url: url,
			type: "POST",
			data: {"queryAdType":0,"bidWord":word,"token":token_value,"campaignId":0,"isAjaxRequest":true,"sla":"json"},
			async: false,
			statusCode: {
				404: function(){
					alert("不好啦，点击下关键词链接，输入下验证码吧！\n 然后重新点击。");
				}
			},
			success: function(html) {
			  ret=html;

			  if(ret.code != 1) { alert("retrieve rank error.");  return 1;} // 如果返回码异常，就退出；
			  
			  for(var i=0; i<ret.result.whoisDOList.length; i++) {
				 if(id == ret.result.whoisDOList[i].linkUrl.match(/\d+$/g).pop()){
					 rank=ret.result.whoisDOList[i].order;
					 break;
				 }
			  }

			}
		 });

		return rank;
}

function oneRowHandler(){

	var word=$(this).parent().children().eq(1).find('a').text();

	var url="http://subway.simba.taobao.com/insight/doWhois.htm";

    var rank=102;
	rank=retrieve_url(url, word, auction_id);
	
	if(rank != 102){ //代表不是没有抓到的情况
		if (rank > 100)
		{
			rank = "101";
		}

		$(this).parent().children().eq(colRank).text(rank).addClass('n'+colRank);

		var color = "background:#F33";

		if(rank <= 100){
			color="background:#6F6";
		}

		$(this).parent().children().eq(colRank).attr('style', color);
	}	
}

function retrieve_page_url(url){
		var search_times_digits;
		var avg_price_digits;
		var rank=288;
		var cat_match_rank=-1;
		var word_first_cat='';

		var page_url=$('#adGroupTitle').parent(':first').attr('href').replace(/http:\/\//, '');
		
		var html = $.ajax({
			url: url,
			async: false,
			statusCode: {
				404: function(){
					alert("不好啦，点击下关键词链接，输入下验证码吧！\n 然后重新点击。");
				}
			},
			success: function(html) {

				var len=$('.yzm', html).length;
				if(len > 0){
					console.log("出现屏蔽了。");
					rank=266;
					return(3);
				}

				var obj1=$('.mb10', html).eq(1);
				var obj2=obj1.next();

				var search_times=obj1.text();
				var avg_price=obj2.text();
		
				// Below gets rank num.
				$('.cr1', html).each(function(i){
						var url = $(this).find('a');
						var inner_url;
						if (url.length > 0)
						{
							inner_url=url.attr('href').replace(/http:\/\//, '');
						}

						var num=$(this).children().eq(0).text();
 	
						if(page_url == inner_url) {
							rank=num;
						}
				})

				if( search_times != undefined ){
					if(search_times.length > 3){
						str_arr=search_times.match(/\s(\d+)\s/g);

						search_times_digits=(str_arr==null)?("错误!"):(str_arr.pop());
						search_times_digits=search_times_digits.replace(/\s/gi,'');

						avg_price_digits=avg_price.match(/\D(\d|\.)*?$/g);
						avg_price_digits=avg_price_digits[0].substr(1);
					} else {
						search_times_digits='网络错误';
						avg_price_digits='网络错误';
					}
				}

			}
		 });

		var arr=new Array();
		arr[0]=search_times_digits;
		arr[1]=avg_price_digits;
		arr[2]=(rank > 100)?101:rank;

		return arr;
}

function pageHandler(){	

	if(auth() == 0) {
		alert("亲，请联系旺旺wssansbb购买！");
		return false;
	}
	
	init_header();

	var url=$(this).parents("tr:first").children("td:nth-child(2)").children('a[href^="doWhois"]').attr('href');
	url='http://subway.simba.taobao.com/bidword/'+url;
	var arr=new Array();
	arr=retrieve_page_url(url);

	var search_times_digits=arr[0];
	var avg_price_digits=arr[1];
	var rank=arr[2];	

	if(search_times_digits != undefined ){
		$(this).parent().children().eq(colSearchTimes).text(search_times_digits);
        $(this).parent().children().eq(colSearchTimes).addClass('n'+colSearchTimes);
		$(this).parent().children().eq(colSearchTimes).attr('style', 'background:#FDF');
	}else {
		$(this).parent().children().eq(colSearchTimes).text("错误!");
		$(this).parent().children().eq(colSearchTimes).attr('style', 'background:#FDF');
	}

	if(avg_price_digits != undefined && avg_price_digits != ""){
		$(this).parent().children().eq(colAvgPrice).text(avg_price_digits);
		$(this).parent().children().eq(colAvgPrice).addClass('n'+colAvgPrice);
		$(this).parent().children().eq(colAvgPrice).attr('style', 'background:#FDF');
	}else {
		$(this).parent().children().eq(colAvgPrice).text("错误!");
		$(this).parent().children().eq(colAvgPrice).attr('style', 'background:#FDF');
	}
	
	if(rank != 266){ //代表不是没有抓到的情况
		if (rank > 100)
		{
			rank = "101";
		}

		$(this).parent().children().eq(colRank).text(rank).addClass('n'+colRank);

		var color = "background:#F33";

		if(rank <= 100){
			color="background:#6F6";
		}
		$(this).parent().children().eq(colRank).attr('style', color);
	}

}


function retrieve_keywords(arg) {
	var len=200;
	var name='abc';
	var str=$('.headright p').eq(1).text();
	if(str.search(/：/)>=0){
		name=str.match(/：.*\)/gi).pop().slice(1,-1);
	} else {
		name=str.match(/，.*\(/gi).pop().slice(1,-1);
	}

	var cat_str=$("div.title>div").eq(0).text();
	var cat=cat_str.match(/：.*$/).pop().slice(1).replace(/\s/gi, '').split('>').join(' > ');
	var url="";

	switch(arg){
		case 1:
			url="http://t.trenddata.cn/back-end/keywords.php?cat="+cat+"&from=0&len="+len;
			break;
		case 2:
			url="http://t.trenddata.cn/back-end/keywords.php?cat="+cat+"&from=200&len="+len;
			break;
		case 3:
			url="http://t.trenddata.cn/back-end/keywords.php?cat="+cat+"&from=400&len="+len;
			break;
		case 4:
			url="http://t.trenddata.cn/back-end/keywords.php?cat="+cat+"&from=600&len="+len;
			break;
		default:			
			url="http://t.trenddata.cn/back-end/keywords.php?cat="+cat+"&from=0&len=20";
			break;			
	}
	url=encodeURI(url);

	window.open(url, 'newwindow', 'height=500, width=800, top=70,left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=yes, status=no');
}

function page_retrieve(from, to) {
	if(auth() == 0) {
		alert("亲，请联系旺旺wssansbb购买！");
		return false;
	}
			
	// 获得全局的 url prefix
	var url = document.location.href;	//获取当前tab文档的url 
	var prefix = dirname(url);		//通过tab文档的url获取 http://subway.simba.taobao.com/bidword
	if(prefix.search(/subway/i) < 0) {
		alert("亲，刷新，等待页面完全载入后再执行。");
	}

	if(url.search(/listPageBidword\.htm/i)<0){
		alert("亲，这个功能需要在页面推广页执行。例如，listPageBidword.htm关键词的页面！");
		return 0;
	}

	
	var page_url=$('#adGroupTitle').parent(':first').attr('href').replace(/http:\/\//, '');
	
	init_header();


	var trs = $('#tb_keyword tr[id^="tr_"]');
	if (from >= 0 && to >= 0) {
		trs = $('tr[id^="tr_"]').slice(from, to);
	}

	var count = 0;
    var total = trs.length;
    
	trs.each(function(i) {
		var ret = "http://subway.simba.taobao.com/bidword/"+ $(this).children().eq(1).children('a').attr("href");
		var tr = $(this);
		$.get(ret, function(html) {
			var len=$('.yzm', html).length;
			if(len > 10){
				console.log("出现屏蔽了。");
				rank = 266;
				return false;
			}
			var obj1 = $('.mb10', html).eq(1);
			var obj2 = obj1.next();

			var search_times = obj1.text();
			var avg_price = obj2.text();

			var search_times_digits;
			var avg_price_digits;
			var rank=266;
			
			// Below gets rank num.
			$('.cr1', html).each(function(i){
					var url = $(this).find('a');
					var inner_url;
					if (url.length > 0)
					{
						inner_url=url.attr('href').replace(/http:\/\//, '');
					}

					var num=$(this).children().eq(0).text();

					if(page_url == inner_url) {
						rank=num;
					}
			})

			// 大于10 表明是有值输出的；
			if(search_times.length > 10) {
				search_times_digits = search_times.match(/\s(\d+)\s/g);

				avg_price_digits = avg_price.match(/\D(\d|\.)*?$/g);
				avg_price_digits = avg_price_digits[0].substr(1);
			} else {
				search_times_digits = '错误!';
				avg_price_digits = '错误!';
			}

			
			if(undefined != search_times_digits){
				tr.children().eq(colSearchTimes).text(search_times_digits.pop());
				tr.children().eq(colSearchTimes).addClass('n'+colSearchTimes);
				tr.children().eq(colSearchTimes).attr('style', 'background:#FDF');
			} else {
				tr.children().eq(colSearchTimes).text("错误");
				tr.children().eq(colSearchTimes).attr('style', 'background:#FDF');
			}

			if ( (avg_price_digits != undefined) && (avg_price_digits != "")){
				tr.children().eq(colAvgPrice).text(avg_price_digits);
				tr.children().eq(colAvgPrice).addClass('n'+colAvgPrice);
				tr.children().eq(colAvgPrice).attr('style', 'background:#FDF');
			} else {
				tr.children().eq(colAvgPrice).text("错误!");
				tr.children().eq(colAvgPrice).attr('style', 'background:#FDF');
			}
			
			if (rank > 100) {
				rank = "101";
			}
			tr.children().eq(colRank).text(rank).addClass('n'+colRank);
			var color = "background:#F33";
			if(rank <= 100){
				color="background:#6F6";
			}
			tr.children().eq(colRank).attr('style', color);

		});
	});
}

function array_index(arr, obj) {
    for(var i=0; i<arr.length; i++) {
        if (arr[i] == obj){
			return i;
		} 
    }
	return 101;
}

function retrieve_category(from, to) {
	var colorArray=["#F7C691","#FBF597","#D2FCA0","#82F8FB","#86F849","#C9CC60","#7E8BFA","#AE75F4","#F448F4","#F0959E","#DB3333", "#63CD29", "#FF6726", "#8023F1", "#F99735", "#A0498D", "#937159", "#C0C0C0", "#D0D6F0"];
	var colDisp=2;
	var row_index=1;

	var url = document.location.href;	//获取当前tab文档的url 
	if(url.search(/adgroup/i) == -1){
		alert("亲，需要在我的直通车，推广计划页面使用这个功能；");
		window.location="http://subway.simba.taobao.com/home.htm?token=&campaignId=0";
		alert("亲，请点击您的推广计划，然后再使用该功能");
		return 0;
	}

	if(auth() == 0) {
		alert("亲，请联系旺旺wssansbb购买！");
		return false;
	}
		
	// 获得全局的 url prefix
	prefix='http://subway.simba.taobao.com/bidword/bidwordmanage.htm?'+url.split('?').pop();	
	prefix=prefix+'&rptDays=1';	

	var trs = $('input[name="adGroupIds"]');
	if (from >= 0 && to >= 0) {
		trs = $('input[name="adGroupIds"]').slice(from, to);
	}

	var count = 0;
	var color;
	
	var item_cat_arr=new Array();

	//this is all page num;
	var page_num=$('div #pr_id>div.pa5').text().replace(/\s*/i,'').split('：').pop();
	var a=page_num/20;
	var loop_times=(a > Math.floor(a))? (Math.floor(a)+1):a;
	// 处理其他页的信息
	var home_table=$('#pio > tbody'); //这里获得起始页的table对象

	for(j=1; j<loop_times; j++){
		$.get(url,{pageNumber:j+1}, function(html) {
				$('#pio > tbody>tr', html).slice(1).each(function(i){
					// 获得类目
					var tr = $(this);
					var grpID=tr.find('input[name="adGroupIds"]').attr('value');
					var url=prefix +'&query.adGroupId='+ grpID;
					$.get(url, function(html) {
						var cat_str=$("div.title>div", html).eq(0).text();
						var cat=cat_str.match(/：.*$/).pop().slice(1);
						var temp=cat.replace(/\s/gi, '');
						var ret=array_index(item_cat_arr, temp);
						if( ret != 101 ) { //在数组中，更新元素颜色；
							color=colorArray[ret];
						} else {
							item_cat_arr.push(temp);
							ret=item_cat_arr.length-1;
							color=colorArray[ret];
						}
						tr.children().eq(colDisp).text(row_index+": "+temp);
						row_index++;
						tr.children().eq(colDisp).attr('style', 'background:'+color);
						home_table.append('<tr>'+tr.html()+'</tr>');
					})
					
				});	
		});
	}    

	trs.each(function(i) {
		var url = prefix +'&query.adGroupId='+ $(this).attr("value");
		var tr = $(this);
		$.get(url, function(html) {
			var cat_str=$("div.title>div", html).eq(0).text();
			var cat=cat_str.match(/：.*$/).pop().slice(1);
			var temp=cat.replace(/\s/gi, '');
			var ret=array_index(item_cat_arr, temp);
			if( ret != 101 ) { //在数组中，更新元素颜色；
				color=colorArray[ret];
			} else {
				item_cat_arr.push(temp);
				ret=item_cat_arr.length-1;
				color=colorArray[ret];
			}

			tr.parent().parent().children().eq(colDisp).text(row_index+": "+temp);
			row_index++;
			tr.parent().parent().children().eq(colDisp).attr('style', 'background:'+color);
		});	
	});
}

function copy_words() {
	var append_str_before='<div width="100%"><textarea  style="margin-top: 0px; margin-bottom: 0px; height:100px; margin-left: 0px; margin-right: 0px; width: 750px;">';
	var append_str_end='</textarea></div>';	

	var words='';
	
	$('td[style$="center"]').each(function(i){
		words=words+$(this).text() + "\n";
	});
	
	var output=append_str_before+words+append_str_end;

	$('#l_tab').before(output);
}

function copy_table() {
	var append_str_before='<div width="100%"><textarea  style="margin-top: 0px; margin-bottom: 0px; height:100px; margin-left: 0px; margin-right: 0px; width: 750px;">';
	var append_str_end='</textarea></div>';	

	var words=$('table.top-table').html();
	words='<table>'+words+'</table>';
		
	var output=append_str_before+words+append_str_end;

	$('#l_tab').before(output);

}

function cube_gets() {
	var append_str_before='<div width="100%"><textarea  style="margin-top: 0px; margin-bottom: 0px; height:100px; margin-left: 0px; margin-right: 0px; width: 750px;">';
	
	var append_str_end='</textarea></div>';	

	var cat_id=new Array();

	$.ajax({
	  url: "http://cube.data.taobao.com/cubex/proxy/p/db/category/dimcategory/get_all_categories",
	  async: false,
	  type: "GET",
	  success:function(html){
		  var arr=html[0];
		  
		  var str="";
		  for (key in arr)
		  {
			cat_id.push(arr[key][0]);
		  }
	  }
	});

	console.log(cat_id);
	var str='';

	for (key in cat_id)
	{
      var urlstr="http://cube.data.taobao.com/cubex/proxy/p/db/keywords/fact_cat_top_query_d/get_taoci_hotkeys_rank_detail/param/tfix:200/where/f0:eq:2012-03-16/where/r1:ge:2012-03-16/where/r1:le:2012-03-16/where/cid:eq:"+cat_id[key]+"/param/dtb:cat?27422-81404-369994";

		$.ajax({
		  url: urlstr,
		  async: true,
		  type: "GET",
		  success:function(html){
			  var ret="";
			  var arr=html[0];
			  for (key in arr)
			  {
				  ret+=arr[key][1]+",";
			  }
			  $('#_table_key_word_l').before(ret);
		  }
		});
	}

}


function seo() {
	var append_str_before='<div width="100%"><textarea  style="margin-top: 0px; margin-bottom: 0px; height:100px; margin-left: 0px; margin-right: 0px; width: 750px;">';
	
	var append_str_end='</textarea></div>';	

	var input=document.getElementById('title');
	var q=input.value;
	q=encodeURI(q);

	var url = 'http://suggest.taobao.com/sug?extras=1&code=utf-8&callback=KISSY.Suggest.callback&q='+q;

	$.ajax({
		  url: url,
		  async: true,
		  type: "GET",
		  success:function(html){
			  ret=html;
			  ret=ret.replace(/KISSY.Suggest.callback\(/g, '').replace(/\)/, '');
			  ret=JSON.parse(ret);
			  var arr=ret.result;

			  var str="";

			  for (key in arr)
			  {
				  str=str+arr[key][0]+"\n";
			  }

			  var append=append_str_before+str+append_str_end;
			  
			  $('div.search-auto').before(append);

		  }
		});

	url='http://suggest.taobao.com/sug?area=b2c&code=utf-8&callback=KISSY.Suggest.callback&q='+q;
	$.ajax({
		  url: url,
		  async: true,
		  type: "GET",
		  success:function(html){
			  ret=html;
			  ret=ret.replace(/KISSY.Suggest.callback\(/g, '').replace(/\)/, '');
			  ret=JSON.parse(ret);
			  var arr=ret.result;

			  var str="";

			  for (key in arr)
			  {
				  str=str+arr[key][0]+"\n";
			  }

			  var append=append_str_before+str+append_str_end;
			  
			  $('div.search-auto').before(append);

		  }
		});
}

//////////////////////////////////////////////////////////////////////////////////////////////
//				主调 区域
//////////////////////////////////////////////////////////////////////////////////////////////
